extends Node

signal dialog_started
signal dialog_finished
signal dialog_next_requested
signal dialog_state_changed(is_in_dialog: bool)

var current_dialog: Array = []
var current_index: int = 0
var is_dialog_active: bool = false

# 对话数据格式示例:
# [
#   { "name": "村长", "text": "你好,旅行者" },
#   { "name": "玩家", "text": "你好,请问这里是什么地方?" }
# ]


func start_dialog(dialog_data: Array) -> void:
	if is_dialog_active:
		return
		
	current_dialog = dialog_data
	current_index = 0
	is_dialog_active = true
	emit_signal("dialog_state_changed", true)
	emit_signal("dialog_started")
	show_current_message()

func show_current_message() -> void:
	if current_index >= current_dialog.size():
		end_dialog()
		return
	
	var message = current_dialog[current_index]
	emit_signal("dialog_next_requested", message)

func next_message() -> void:
	if not is_dialog_active:
		return
		
	current_index += 1
	show_current_message()

func end_dialog() -> void:
	is_dialog_active = false
	current_dialog.clear()
	current_index = 0
	emit_signal("dialog_state_changed", false)
	emit_signal("dialog_finished") 
